// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var js_platform_main = require("./platform_main.js");
	var css_receivableNoticeDetail = require("../financing/receivableNoticeDetail.css");
	var pageIndex = 1,
		pageSize = 5,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var imgSrc;
	var auditAmount;
	var receivedAmount;
	var pickupId;
	var totalLoanAmount;
	var fileUuid;
	var loanNoticeId;
	var receivablesId;
	var left;
	var enterpriseId;
	var init = function(page) {
		enterpriseId = page.params.enterpriseId || base.getUrlParam("enterpriseId");
		receivedAmount = page.params.receivedAmount || base.getUrlParam("receivedAmount");
		auditAmount = page.params.auditAmount || base.getUrlParam("auditAmount");
		pickupId = page.params.pickupId || base.getUrlParam("pickupId");
		left = page.params.left || base.getUrlParam("left");
		var noReceivedAmount = auditAmount - receivedAmount;
		$(".noReceivedAmount").html("￥" + noReceivedAmount);
		$(".auditAmount").html("￥" + auditAmount);
		$(".receivedAmount").html("￥" + receivedAmount);
		databind.fillForm($(".receivablesContent"), {
			pickupId: pickupId
		});
		$(".left").removeClass("none");
		leftMenu.init(6);
		$(".table-section").removeClass("none");
		$(".footer").removeClass("none");
		$("#main").css("background", "#ffffff");
		$("#leftMenu7").find(".leftMenu-item").removeClass("leftMenu-active");
		$("#leftMenu7 .leftMenu li:first-child").addClass("leftMenu-open");
		$("#leftMenu7").find(".leftMenu-item").eq(left).addClass("leftMenu-active");
		js_platform_main.init(0);
		inquireEnterpriseDetail();
//		inquireSystemBlankInfo();
		inquireBlankInfoPagelist();
		initEvent();
		router.init();
	};
	var initEvent = function(page) {

		if (page != null) {
			receivablesId = page.params.receivablesId;
			databind.fillInput($("#receivablesNotice_receivables"), {
				action: "inquireReceivablesDetail.json?receivablesId=" + receivablesId
			});
		}

		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		//点击首页
		$(".platform_main").unbind("click").click(function() {
			top.location.href = "../main/platform_main.html";
		});

		//点击收款列表
		$(".receivablesNoticeList").unbind("click").click(function() {
			router.route("main/receivablesNotice_list.html?left=7", true);
		});
		//点击收款详情
		$(".receivablesNotice").unbind("click").click(function() {
			router.route("main/receivablesNotice.html?pickupId=" + pickupId + "&left=" + left, true);
		});
		//点击上传附件
		$("#receivablesNotice_receivables").on("change", "#loanIntro", function() {
			uploadFile();
		});

		$("#receivablesNotice_receivables").on("click", ".deleteLoanIntro", function() {
			$(this).parent().remove();
		});
		
		$("#receivablesNotice_receivables select[name=accountInfoId]").on("change", function(){
			var $this = $(this);
			$("#receivablesNotice_receivables input[name=beneficiaryBank]").val($("select[name=accountInfoId]").find("option:selected").attr("accountbank"));
			$("#receivablesNotice_receivables input[name=beneficiaryName]").val($("select[name=accountInfoId]").find("option:selected").attr("accountBankName"));
			$("#receivablesNotice_receivables input[name=beneficiaryCreditCard]").val($("select[name=accountInfoId]").find("option:selected").attr("accountBankCard"));
		});

		//点击理货 详情
		$("#tallyingListBody").on("click", ".tallyingListDetail", function() {
			var inventoryId = [];
			var $this = $(this).parents("tr.tr_style");
			inventoryId.push($this.attr("inventoryId"));
			router.route("financing/tallyDetail.html?inventoryId=" + inventoryId + "&recieveNoticeId=" + recieveNoticeId + "&type=readOnly&financeId=" + financeId, true);

		});

		//点击放款确认
		$("#receivablesLoanSubmit").on("click", function() {

			databind.submitDom($(".receivablesContent"), function() {
				base.alert(1, "收款确认成功");
				router.route("main/receivablesNotice_list.html?left=7", true);
			});
		});
	};

	//查询平台银行账户
	var inquireSystemBlankInfo = function() {
			base.fnAjax({
				action: "inquireSystemBlankInfo.json",
				oData: {}, //传递参数
				fnSuccess: function(data) {
					if (data.status == 200) {

						$("#beneficiaryName").val(data.datas[0].accountHolder);
						$("#beneficiaryBank").val(data.datas[0].depositBank);
						$("#beneficiaryCreditCard").val(data.datas[0].creditCard);
						data.datas[0].accountHolder;
						data.datas[0].creditCard;
					} else {
						base.alert(2, "信息获取失败");
					}
				}
			});
		}
		//查询企业明细 --企业银行账户
	var inquireEnterpriseDetail = function() {
		base.fnAjax({
			action: "inquireEnterpriseDetail.json",
			oData: {
				enterpriseId: enterpriseId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					//					$("#beneficiaryName").val(data.datas[0].accountHolder);
					$("#paymentBank").val(data.datas[0].depositBank);
					$("#paymentCreditCard").val(data.datas[0].creditCard);
					data.datas[0].accountHolder;
					data.datas[0].creditCard;
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}
	
	//查询账户列表
	var inquireBlankInfoPagelist = function() {
		base.fnAjax({
			action: "inquireBlankInfoPagelist.json",
			oData: {
				pageSize: 1000
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					$.each(data.datas, function() {
						var str = "<option accountBankName='"+ this.accountBankName +"' accountBankCard='"+ this.accountBankCard +"' accountBank='"+ this.accountBank +"' value='"+ this.accountInfoId +"'>" + this.accountBank + "(" + this.accountBankName + ")" + "</option>";
						$("select[name=accountInfoId]").append(str);
					});
					
					$("#receivablesNotice_receivables input[name=beneficiaryBank]").val($("select[name=accountInfoId]").find("option:selected").attr("accountbank"));
					$("#receivablesNotice_receivables input[name=beneficiaryName]").val($("select[name=accountInfoId]").find("option:selected").attr("accountBankName"));
					$("#receivablesNotice_receivables input[name=beneficiaryCreditCard]").val($("select[name=accountInfoId]").find("option:selected").attr("accountBankCard"));
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	//上传文件
	var uploadFile = function() {
		if ($("#loanIntro").val() != "") {
			$.ajaxFileUpload({
				url: "/finance/api/v1.0/uploadFile.json", //你处理上传文件的服务端
				secureuri: false,
				fileElementId: 'loanIntro',
				dataType: 'json',
				success: function(data) {
					var str = '<div class="showloanIntroDiv"><a target="_blank" class="mas showloanIntro" id="' + data.data[0].fileUuid + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + data.data[0].fileUuid + '" >' + data.data[0].fileName + '</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="deleteLoanIntro">x</a></div>'
					$(".loanIntro").before(str);
					saveMaterials();
				},
				complete: function(xml) {
					$("#loanIntro").change(function() {
						uploadFile();
					});
				},
				error: function(data, status, e) {
					if (data.msg == null) {
						base.alert(2, "文件太大无法上传");
					} else {
						base.alert(2, data.msg);
					}
				}
			});
		} else {
			base.alert(3, "请选择文件！");
		}
	};

	//更新fileUuuid保存字段
	var saveMaterials = function() {
		var materials = "";
		var number = $(".showloanIntro").length;
		$(".showloanIntro").each(function(i, object) {
			if (i == number - 1) {
				materials += object.id;
			} else {
				materials += object.id + ",";
			}

		});
		$("#fileUuids").val(materials);
	}

	return {
		init: init
	};
});